#include "stdafx.h"
#include "hzCommander.h"

using namespace hz;

const int LIST_BOX_ID	= 101;
const int ORDER_BOX_ID	= 102;
const int EDIT_BOX_ID	= 103;



commander::commander(const wstring& title) 
: _hWndListBox(NULL), _hWndOrder(NULL), _hWndEdit(NULL), _hWndSaveCheck(NULL), _OutputLine(0) {
	_Title = title;
	Init();
}

bool commander::Init(void) {
	HINSTANCE hInst = GetModuleHandle(NULL);

	if(window::Init(hInst, _T("Commander"), _Title.c_str(), NULL, 0, 0, 640, 480, false, 
		WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION | WS_BORDER | WS_SYSMENU) == false)
		return false;
	
	_hWndListBox = CreateWindow(_T("LISTBOX"), NULL, 
		WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL ,
		0, 0, 640, 420, window::GetHWND(), (HMENU)LIST_BOX_ID, 
		hInst, NULL);

	_hWndOrder = CreateWindow(_T("edit"), NULL, 
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY,
		0, 420, 640, 20, window::GetHWND(), (HMENU)ORDER_BOX_ID, 
		hInst, NULL);

	_hWndEdit = CreateWindow(_T("edit"), NULL, 
		WS_CHILD | WS_VISIBLE | WS_BORDER,
		0, 440, 640, 20, window::GetHWND(), (HMENU)EDIT_BOX_ID, 
		hInst, NULL);

	_hWndSaveCheck = CreateWindow(_T("button"), _T("Text Save?"), 
		WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
		620, 465, 12, 12, window::GetHWND(), NULL, 
		hInst, NULL);

	ShowWindow(window::GetHWND(), SW_HIDE);

	return true;	
}

void commander::Show(void) {
	assert(window::GetHWND() != NULL);

	ShowWindow(window::GetHWND(), SW_SHOW);
}

void commander::Hide(void) {
	assert(window::GetHWND() != NULL);

	ShowWindow(window::GetHWND(), SW_HIDE);
}

void commander::Out(wchar_t* format, ...) {
	if(_hWndListBox == NULL)
		return;

	va_list arg;
	va_start( arg, format );

	wchar_t Buffer[1024];
	vswprintf( Buffer, 1024, format, arg );

	OutString(Buffer);

	va_end(arg);
}

void commander::OutString(const wchar_t* string) {
	if(_hWndListBox == NULL)
		return;

	SendMessage( _hWndListBox, LB_ADDSTRING, 0, (LPARAM) string);
	SendMessage( _hWndListBox, LB_SETCURSEL, (WPARAM)_OutputLine, 0L );
	++_OutputLine;
}

bool commander::InCheck(void) {
	if(_hWndEdit == NULL)
		return false;

	return !(_InString.empty());
}

std::wstring commander::In(void) {
	if(_hWndEdit == NULL)
		return NULL;

	std::wstring Temp;
	Temp.swap(_InString);	
	return Temp;
}

void commander::Save(void) {
	if(_hWndListBox == NULL)
		return;

	if(SendMessage(_hWndSaveCheck, BST_CHECKED, NULL, NULL) == BST_UNCHECKED)
		return;

	wchar_t Buffer[1024];

	swprintf(Buffer, _T("_%d.txt"), GetTickCount());
	wstring FileName = _Title + Buffer;	
	
	FILE* fp = NULL;
	fp = _wfopen(FileName.c_str(), _T("a+"));
	if(fp == NULL)
		return;
	
	for(unsigned int Index = 0; Index < _OutputLine; Index++) {
		SendMessage(_hWndListBox, LB_GETTEXT, Index, (LPARAM)Buffer);
		fwprintf(fp, _T("%s\n"), Buffer);
	}

	fclose(fp);
}


bool CALLBACK commander::CustomProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	static wchar_t Buffer[1024];

	switch(message) {
		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case EDIT_BOX_ID:
					switch(HIWORD(wParam)) {
						case EN_CHANGE:
							SetTimer(hWnd, 0, 50, NULL);
							break;							
					}
					break;
			}
			break;

		case WM_TIMER:
			if(GetAsyncKeyState(VK_RETURN) & 0xff00) {
				GetWindowText(_hWndEdit, Buffer, 1024);
				SetWindowText(_hWndEdit, _T(""));
				SetWindowText(_hWndOrder, Buffer);
				_InString.assign(Buffer, Buffer + wcslen(Buffer));
				KillTimer(hWnd, 0);
			}
			break;

		case WM_DESTROY:
			break;
		
		default:
			DefWindowProc( hWnd, message, wParam, lParam );
	}
	
	return false;
}

